<template>
  <div style="width: 100%; height: 100%">
    <component
      v-if="templateShow"
      :preview-data="formData"
      :is="currentComponent"
      ref="sublevel"
    />
    <div class="no-data" v-else>暂无模板样式</div>
  </div>
</template>

<script>
import { detailsTemplate } from "@/api/templateManage/index";
import { planListDetails } from "@/api/planManage/index.js";
import Template_1 from "./src/Template_1.vue";
import Template_2 from "./src/Template_2.vue";
import Template_3 from "./src/Template_3.vue";
import Template_4 from "./src/Template_4.vue";
import quotation from "./otherTemplate/quotation.vue";
import MarketingProgram from "./otherTemplate/MarketingProgram.vue";
import Video_360 from "./otherTemplate/Video_360.vue";
import Image_360 from "./otherTemplate/Image_360.vue";
import Video from "./otherTemplate/Video.vue";
export default {
  name: "TemplateWindow",
  components: {
    Template_1,
    Template_2,
    Template_3,
    Template_4,
    quotation,
    MarketingProgram,
    Video_360,
    Image_360,
    Video
  },
  props: {
    formData: {
      type: Object,
      default: null,
    },
    templateType: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      currentComponent: "Template_1",
      templateShow: true,
      pageName: {
        "/views/EnterpriseShow/ManyPic/index": "Template_1", //多图静态展示
        "/views/EnterpriseShow/EnterpriseProfile/index": "Template_2", // 企业简介
        "/views/EnterpriseShow/Carousel/index": "Template_3", // 轮播图
        "/views/EnterpriseShow/ImagePromotionFilm/index": "Template_4", // 视频播放
      },
    };
  },
  methods: {
    init() {
      if (this.templateType === '个人空间-营销方案-上架' || this.templateType === '个人空间-营销方案-下架') {
        this.currentComponent = 'MarketingProgram';
      } else if (this.templateType === '产品货架-报价方案-上架' || this.templateType === '产品货架-报价方案-下架') {
        this.currentComponent = 'quotation';
        this.handleQuotationDetails();
      } else if (this.templateType === '360视频') {
        this.currentComponent = 'Video_360';
      } else if (this.templateType === '360图片') {
        this.currentComponent = 'Image_360';
      } else if (this.templateType === '视频') {
        this.currentComponent = 'Video';
      } else {
        detailsTemplate(this.formData.templateId).then((res) => {
          if (Boolean(this.pageName[res.data.mappingUrl]) === true) {
            this.templateShow = true;
            this.currentComponent = this.pageName[res.data.mappingUrl];
          } else {
            this.templateShow = false;
          }
        });
      }
    },
    // 处理报价方案详情
    handleQuotationDetails() {
      planListDetails(this.formData.pageId).then((res) => {
        this.$refs.sublevel.listData = [];
        this.$refs.sublevel.total = 0;
        this.$refs.sublevel.motorcount = res.data.motorCarNum
        this.$refs.sublevel.trailercount = res.data.trailerNum
        const data = res.data.quote;
        data.forEach(item => {
          console.log();
          if (item.groupMethod === "1" && item.configurationType === "1") {
            item.sumDomesticGoodsPrice = item.nums * item.domesticGoodsPrice;
            item.sumFreeImportGoodsPrice = item.nums * item.freeImportGoodsPrice;
            item.sumNoFreeImportGoodsPrice = item.nums * item.noFreeImportGoodsPrice;
            item.total = item.sumDomesticGoodsPrice + item.sumFreeImportGoodsPrice + item.sumNoFreeImportGoodsPrice;
            this.$refs.sublevel.motorBase.push(item);
          } else if (item.groupMethod === "1" && item.configurationType === "2") {
            item.sumDomesticGoodsPrice = item.nums * item.domesticGoodsPrice;
            item.sumFreeImportGoodsPrice = item.nums * item.freeImportGoodsPrice;
            item.sumNoFreeImportGoodsPrice = item.nums * item.noFreeImportGoodsPrice;
            item.total = item.sumDomesticGoodsPrice + item.sumFreeImportGoodsPrice + item.sumNoFreeImportGoodsPrice;
            this.$refs.sublevel.motorChoose.push(item);
          } else if (item.groupMethod === "2" && item.configurationType === "1") {
            item.sumDomesticGoodsPrice = item.nums * item.domesticGoodsPrice;
            item.sumFreeImportGoodsPrice = item.nums * item.freeImportGoodsPrice;
            item.sumNoFreeImportGoodsPrice = item.nums * item.noFreeImportGoodsPrice;
            item.total = item.sumDomesticGoodsPrice + item.sumFreeImportGoodsPrice + item.sumNoFreeImportGoodsPrice;
            this.$refs.sublevel.trailerBase.push(item);
          } else if (item.groupMethod === "2" && item.configurationType === "2") {
            item.sumDomesticGoodsPrice = item.nums * item.domesticGoodsPrice;
            item.sumFreeImportGoodsPrice = item.nums * item.freeImportGoodsPrice;
            item.sumNoFreeImportGoodsPrice = item.nums * item.noFreeImportGoodsPrice;
            item.total = item.sumDomesticGoodsPrice + item.sumFreeImportGoodsPrice + item.sumNoFreeImportGoodsPrice;
            this.$refs.sublevel.trailerChoose.push(item);
          }
          this.$refs.sublevel.total += item.total;
        })
        this.$refs.sublevel.getCArList('motorCarNum');
        this.$refs.sublevel.getCArList('trailerNum');
      })
    }
  },
  mounted() {
    this.init();
  },
};
</script>

<style scoped lang="scss">
.no-data {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  font-weight: 600;
}
</style>